﻿using System;
using System.IO.IsolatedStorage;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace XamlingCore.Infrastructure.OOB
{
    public class WindowStateManager
    {
        private IsolatedStorageSettings appSettings =
            IsolatedStorageSettings.ApplicationSettings;

        private bool saveOnExit;

        public WindowStateManager(bool saveOnExit)
        {
            this.saveOnExit = saveOnExit;
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
            {
                Application.Current.Exit += new EventHandler(Current_Exit);
                Application.Current.MainWindow.Closing += new EventHandler<System.ComponentModel.ClosingEventArgs>(MainWindow_Closing);
            }

        }

        void Current_Exit(object sender, EventArgs e)
        {
            saveSettings();
        }

        public void RestoreWindowPosition()
        {
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
            {
                var top = Top;
                var left = Left;

                if (top != -1 && left != -1)
                {

                    var wind = Application.Current.MainWindow;
                    wind.Top = top;
                    wind.Left = left;


                }
            }
        }

        void MainWindow_Closing(object sender, System.ComponentModel.ClosingEventArgs e)
        {
           
            saveSettings();
        }

        void saveSettings()
        {
            if (Application.Current.IsRunningOutOfBrowser && Application.Current.HasElevatedPermissions)
            {
                if (saveOnExit)
                {
                    var wind = Application.Current.MainWindow;

                    var top = wind.Top;
                    var left = wind.Left;

                    Top = top;
                    Left = left;
                }
            }
        }


        public Double Top
        {
            get
            {
                if (appSettings.Contains("windowTop"))
                {
                    return Convert.ToDouble(appSettings["windowTop"]);
                }
                return -1;
            }
            set
            {
                appSettings["windowTop"] = value;
                appSettings.Save();
            }

        }

        public Double Left
        {
            get
            {
                if (appSettings.Contains("windowLeft"))
                {
                    return Convert.ToDouble(appSettings["windowLeft"]);
                }
                return -1;
            }
            set
            {
                appSettings["windowLeft"] = value;
                appSettings.Save();
            }

        }
    }
}
